package com.example.pinkcat.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.Userpage;
import com.example.pinkcat.activity.VideoActivity;
import com.example.pinkcat.activity.VideoPinkcat;
import com.example.pinkcat.activity.api.entity.VideoEntity;
import com.example.pinkcat.activity.api.entity.VideoInfoEntity;
import com.example.pinkcat.activity.api.getData.GetData;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;
import com.rishabhharit.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

public class VP1_recyleview_adapter extends RecyclerView.Adapter<VP1_recyleview_adapter.ViewHolder>{
    private final Context context;
    String up_id="-1";
    List<VideoEntity> videoEntities=new ArrayList<>();
    VideoInfoEntity videoInfoEntity;//第一个视频简介信息
    VideoView videoView;
    String video_id;

    //构造方法
    public VP1_recyleview_adapter(Context context,String up_id,VideoView videoView,String video_id){
        this.context=context;
        this.up_id=up_id;
        this.videoView=videoView;
        this.video_id=video_id;
    }

    public void setxiangguanData(List<VideoEntity> videoEntities){
        if (videoEntities!=null){
            List<VideoEntity> videos=videoEntities;
            for (VideoEntity entity:videos){
                if (entity.getVideo_part_mp()==1){
                    this.videoEntities.add(entity);
                }
            }
        }
        notifyDataSetChanged();
    }

    //设置视频信息
    public void setvideoinfoData(VideoInfoEntity videoInfoEntity){
        if (videoInfoEntity!=null)
            this.videoInfoEntity=videoInfoEntity;
        notifyDataSetChanged();
    }

    //内部内
    public static class ViewHolder extends RecyclerView.ViewHolder{
        //简介
        TextView video_title;//视频标题
        TextView video_palynum;//视频播放数量
        TextView video_pl_num;//视频评论数量
        TextView video_sendtime;//发送时间
        TextView video_introduction;//视频简介
        ImageView likebtn;//点赞按钮
        TextView likebtn_num;//点赞数量
        ImageView unlikebtn;//点踩按钮
        TextView unlikebtn_num;//点踩数量
        ImageView followbtn;//收藏按钮
        TextView followbtn_num;//收藏数量
        ImageView sharebtn;//分享按钮
        View video_page_introduction;//整个页面

        //
        View aboutvideo;//推荐视频layoutview
        RoundedImageView headimg;//封面
        TextView v_title;//标题
        TextView v_username;//发布者名称
        TextView v_play;//播放量
        TextView v_pl;//评论量


        RecyclerView recyclerView_tv;//集数列表

        //寻找声明布局控件
        public ViewHolder(View view) {
            super(view);
            video_page_introduction=view.findViewById(R.id.video_page_introduction);
            video_title=view.findViewById(R.id.video_title);
            video_palynum=view.findViewById(R.id.bfln);
            video_pl_num=view.findViewById(R.id.plln);
            video_sendtime=view.findViewById(R.id.sned_data);
            video_introduction=view.findViewById(R.id.video_driction);
            likebtn=view.findViewById(R.id.v_like);
            likebtn_num=view.findViewById(R.id.v_like_num);
            unlikebtn=view.findViewById(R.id.v_unlike);
            unlikebtn_num=view.findViewById(R.id.v_unlike_num);
            followbtn=view.findViewById(R.id.v_follow);
            followbtn_num=view.findViewById(R.id.v_follow_num);
            sharebtn=view.findViewById(R.id.v_share);

            aboutvideo=view.findViewById(R.id.list_main_body);
            headimg=view.findViewById(R.id.video_face);
            v_title=view.findViewById(R.id.about_v_title);
            v_username=view.findViewById(R.id.about_username);
            v_play=view.findViewById(R.id.about_bfsl);
            v_pl=view.findViewById(R.id.about_plsl);

            recyclerView_tv=view.findViewById(R.id.xuanji_recycleview);//集数列表
        }
    }
    //创建获取 view  布局
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取item内容
        View view;
        if (viewType==0) {
            view= LayoutInflater.from(context).inflate(R.layout.list_videosimpleintroduce_fortv,parent,false);
        }
        else {
            view= LayoutInflater.from(context).inflate(R.layout.list_aboutvideo,parent,false);
        }
        return new ViewHolder(view);
    }

    //绑定数据
    @SuppressLint({"CheckResult", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //页面1
        if (position==0){//简介头
            if(videoInfoEntity!=null){//初始化简介
                holder.video_title.setText(videoInfoEntity.getVideo_name());
                holder.video_palynum.setText(videoInfoEntity.getTotalplay());
                holder.video_pl_num.setText(videoInfoEntity.getComment_count());
                String[] ss=videoInfoEntity.getVideo_pub_time().split(" ");
                holder.video_sendtime.setText(ss[0]);//时间
                holder.video_introduction.setText(videoInfoEntity.getVideo_info());
                holder.likebtn_num.setText(videoInfoEntity.getLike_num());
                holder.unlikebtn_num.setText(videoInfoEntity.getUnlike_num());
                holder.followbtn_num.setText(videoInfoEntity.getCollect_num());
                holder.sharebtn.setSelected(false);//点赞按钮
                if (userStaticMsg.userId.equals("-1")){//未登陆状态
                    holder.likebtn.setSelected(false);//点赞按钮
                    holder.unlikebtn.setSelected(false);//点赞按钮
                    holder.followbtn.setSelected(false);//点赞按钮
                }
                else {//登陆状态
                    if (videoInfoEntity.getLike()==null||videoInfoEntity.getLike().equals("0")){//未点赞未点踩
                        holder.likebtn.setSelected(false);//点赞按钮
                        holder.unlikebtn.setSelected(false);//点赞按钮
                    }else if (videoInfoEntity.getLike().equals("1")){
                        holder.likebtn.setSelected(true);//点赞按钮
                        holder.unlikebtn.setSelected(false);//点赞按钮
                    }
                    else {
                        holder.likebtn.setSelected(false);//点赞按钮
                        holder.unlikebtn.setSelected(true);//点赞按钮
                    }

                    if (videoInfoEntity.getCollect()==null){//未收藏
                        holder.followbtn.setSelected(false);//点赞按钮
                    }else {
                        holder.followbtn.setSelected(true);//点赞按钮
                    }
                }

            }
            listen_item1(holder);//事件监听
            //为选集添加适配器
            holder.recyclerView_tv.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL,false));
            VP1_recyleview_adapter_underrecycle adapters=new VP1_recyleview_adapter_underrecycle(context,20,holder.recyclerView_tv,videoView);
            holder.recyclerView_tv.setAdapter(adapters);
            GetData.gettvlistvyid(context,video_id,adapters);
        }
        else {//推荐视频list
            //为视频初始化数据
            if (videoEntities.get(position-1)!=null){
                Glide.with(context).load(Uri.parse(videoEntities.get(position-1).getVideo_cover())).into(holder.headimg);
                holder.v_title.setText(videoEntities.get(position-1).getVideo_name());
                holder.v_username.setText(videoEntities.get(position-1).getUser_name());
                holder.v_play.setText(String.valueOf(videoEntities.get(position-1).getTotalplay()));
                holder.v_pl.setText(String.valueOf(videoEntities.get(position-1).getComment_count()));
            }

            //点击监听事件
            holder.aboutvideo.setOnClickListener(new View.OnClickListener() {//点击事件
                @Override
                public void onClick(View v) {
                    if (videoEntities.get(position-1).getVideo_type().equals("综艺")||
                            videoEntities.get(position-1).getVideo_type().equals("电视剧")||
                            videoEntities.get(position-1).getVideo_type().equals("电影")||
                            videoEntities.get(position-1).getVideo_type().equals("动漫"))
                    {
                        Intent intent=new Intent(context, VideoPinkcat.class);
                        intent.putExtra("v_id",videoEntities.get(position-1).getVideo_id());
                        intent.putExtra("up_id",videoEntities.get(position-1).getUser_id());
                        intent.putExtra("v_link",videoEntities.get(position-1).getVideo_link());
                        intent.putExtra("v_labs",videoEntities.get(position-1).getVideo_lab());
                    }
                    else {
                        Intent intent=new Intent(context, VideoActivity.class);
                        intent.putExtra("v_id",videoEntities.get(position-1).getVideo_id());
                        intent.putExtra("up_id",videoEntities.get(position-1).getUser_id());
                        intent.putExtra("v_link",videoEntities.get(position-1).getVideo_link());
                        intent.putExtra("v_labs",videoEntities.get(position-1).getVideo_lab());
                        context.startActivity(intent);
                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return videoEntities.size()+1;
    }


    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return 0;
        }else {
            return 1;
        }
    }


    void listen_item1(ViewHolder holder){
        //关注按钮
        holder.likebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.likebtn.isSelected()){
                    Toast.makeText(context,"已经点赞过了",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(GetData.video_like_follow_un(context,videoInfoEntity.getVideo_id(),"like")){
                    holder.likebtn.setSelected(true);
                    holder.unlikebtn.setSelected(false);
                }
            }
        });//点赞事件
        holder.unlikebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.unlikebtn.isSelected()){
                    Toast.makeText(context,"已经踩过了",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(GetData.video_like_follow_un(context,videoInfoEntity.getVideo_id(),"unlike")){
                    holder.likebtn.setSelected(false);
                    holder.unlikebtn.setSelected(true);
                }
            }
        });//点踩事件
        holder.followbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.followbtn.isSelected()){
                    if(GetData.video_like_follow_un(context,videoInfoEntity.getVideo_id(),"unfollow")){
                        holder.followbtn.setSelected(false);
                    }
                }
                else {
                    if(GetData.video_like_follow_un(context,videoInfoEntity.getVideo_id(),"follow")){
                        holder.followbtn.setSelected(true);
                    }
                }
            }
        });//收藏事件
        holder.sharebtn.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ShowToast")
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"感谢分享~",Toast.LENGTH_SHORT).show();
            }
        });//分享事件
    }
}
